package com.demonzym.gtv.xml.converter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.demonzym.gtv.metadata.Channel;
import com.demonzym.gtv.metadata.TimeList;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class TimeListConverter implements Converter {

	@Override
	public boolean canConvert(Class clazz) {
		// TODO Auto-generated method stub
		return clazz.equals(TimeList.class);
	}

	@Override
	public void marshal(Object arg0, HierarchicalStreamWriter writer,
			MarshallingContext context) {
		// TODO Auto-generated method stub

	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context) {
		// TODO Auto-generated method stub
		TimeList list = new TimeList();
		reader.moveDown();//To child
		list.setTitle(reader.getAttribute("title"));
		list.setNowPlay(reader.getAttribute("nowPlay"));
		list.setNextPlay(reader.getAttribute("nextPlay"));
		reader.moveUp();//To parent
		reader.moveDown();// to next child
		List<Channel> channelList = new ArrayList<Channel>();
		Map<String, String> m = new LinkedHashMap<String, String>();
		while(reader.hasMoreChildren()){
			reader.moveDown();
			m.put(reader.getAttribute("playTime"), reader.getAttribute("tvProgram"));
			reader.moveUp();
		}
		list.setPrograms(m);
		return list;
	}

}